package test.com.newproject.moudle.main.homepage;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Rect;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import test.com.newproject.BR;
import test.com.newproject.R;
import test.com.newproject.framework.mvp.BaseMvpFragment;
import test.com.newproject.framework.mvp.BasePresenter;
import test.com.newproject.framework.ui.BaseAdapter;
import test.com.newproject.model.beans.DataBean;
import test.com.newproject.moudle.common.CommonPresenter;
import test.com.newproject.moudle.common.ICommonContract;

/**
 * Created by zjh on 2017/8/22.
 */
public class HomepageFragment extends BaseMvpFragment implements IHomepageContract.IView, ICommonContract.IView, OnRefreshListener {
    @BindView(R.id.fragment_homepage_srl)
//    protected SwipeRefreshLayout srl;
    protected SmartRefreshLayout srl;
    @BindView(R.id.fragment_homepage_rv)
    protected RecyclerView homepageRv;

    protected HomepageAdapter adapter;
    protected String TAG = "debug";

    protected ViewDataBinding bind;

    @Override
    protected int obtainLayout() {
        return R.layout.fragment_homepage;
    }

    @Override
    public void configPresenter() {
        createPresenter(HomepagePresenter.class, CommonPresenter.class);
    }

    @Override
    protected void initView() {
        super.initView();
        bind = DataBindingUtil.bind(contentView);
//        srl.setColorSchemeResources(R.color.colorPrimary);
        srl.setRefreshHeader(new ClassicsHeader(getActivity()));
        srl.setOnRefreshListener(this);
        homepageRv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        homepageRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.top = 15;
                outRect.bottom = 15;
            }
        });

        adapter = new HomepageAdapter(R.layout.item_homepage);
        adapter.setEnableLoadMore(true);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                homepageRv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtils.showLong("已无更多条目");
                        adapter.loadMoreEnd(true);
                    }
                }, 2000);
            }
        }, homepageRv);

        homepageRv.setAdapter(adapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        srl.autoRefresh();
    }

    @Override
    public void refreshData(List<DataBean> result) {
        bind.setVariable(BR.data, result.get(0));
        adapter.setNewData(result);
        srl.finishRefresh();
    }

    @Override
    public void requestFailed(String err) {
        Log.d(TAG, "requestFailed: " + err);
        Toast.makeText(getContext(), err, Toast.LENGTH_LONG).show();
        srl.finishRefresh();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        HomepagePresenter homepagePresenter = getPresenter(HomepagePresenter.class);
        homepagePresenter.requestData();

        CommonPresenter commonPresenter = getPresenter(CommonPresenter.class);
        commonPresenter.requestCommonData();
    }

    @Override
    public void refreshCommonData() {
        LogUtils.dTag("debug", "refreshCommonData");
    }

    class HomepageAdapter extends BaseAdapter<DataBean, BaseViewHolder> {

        public HomepageAdapter(@LayoutRes int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, DataBean item) {
            helper.setText(R.id.item_main_context_tv, item.getContext());
            helper.setText(R.id.item_main_time_tv, item.getTime());
        }
    }
}
